import mock from '../mock'

// ** Utils
import { paginateArray } from '../utils'

// Avatar Import
import avatar1 from '@src/assets/images/avatars/1-small.png'
import avatar2 from '@src/assets/images/avatars/2-small.png'
import avatar3 from '@src/assets/images/avatars/3-small.png'
import avatar4 from '@src/assets/images/avatars/4-small.png'
import avatar5 from '@src/assets/images/avatars/5-small.png'
import avatar6 from '@src/assets/images/avatars/6-small.png'
import avatar7 from '@src/assets/images/avatars/7-small.png'
import avatar8 from '@src/assets/images/avatars/8-small.png'
import avatar9 from '@src/assets/images/avatars/9-small.png'
import avatar10 from '@src/assets/images/avatars/10-small.png'

const data = {
  invoices: [
    {
      id: 4987,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4988,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4989,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4990,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4991,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4992,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4993,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4994,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4995,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4996,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4997,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4998,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 4999,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 5000,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 5001,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 5002,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 5003,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
    {
      id: 5004,
      name: '李某',
      account: '13527766611',
      dueDate: '2023-09-08 12:00',
    },
  ],
}

// ------------------------------------------------
// GET: Return Invoice List
// ------------------------------------------------
mock.onGet('/apps/invoice/invoices').reply((config) => {
  // eslint-disable-next-line object-curly-newline
  const {
    q = '',
    perPage = 10,
    page = 1,
    status = null,
    sort,
    sortColumn,
  } = config
  return [
    200,
    {
      allData: data.invoices,
      total: data.invoices.length,
      invoices: data.invoices,
    },
  ]
})

// ------------------------------------------------
// GET: Return Single Invoice
// ------------------------------------------------
mock.onGet(/\/api\/invoice\/invoices\/\d+/).reply((config) => {
  // // Get event id from URL
  const invoiceId = Number(
    config.url.substring(config.url.lastIndexOf('/') + 1)
  )

  const invoiceIndex = data.invoices.findIndex((e) => e.id === invoiceId)
  const responseData = {
    invoice: data.invoices[invoiceIndex],
    paymentDetails: {
      totalDue: '$12,110.55',
      bankName: 'American Bank',
      country: 'United States',
      iban: 'ETD95476213874685',
      swiftCode: 'BR91905',
    },
  }
  return [200, responseData]
})

// ------------------------------------------------
// DELETE: Deletes Invoice
// ------------------------------------------------
mock.onDelete('/apps/invoice/delete').reply((config) => {
  // Get invoice id from URL
  let invoiceId = config.id

  // Convert Id to number
  invoiceId = Number(invoiceId)

  const invoiceIndex = data.invoices.findIndex((t) => t.id === invoiceId)
  data.invoices.splice(invoiceIndex, 1)

  return [200]
})

// ------------------------------------------------
// GET: Return Clients
// ------------------------------------------------
mock.onGet('/api/invoice/clients').reply(() => {
  const clients = data.invoices.map((invoice) => invoice.client)
  return [200, clients.slice(0, 5)]
})
